/*
 * painel.java
 *
 * Created on 12. November 2008, 09:44
 */

package GUI;

import java.awt.Point;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  Michael
 */
 class CustomEditableCellTableModel extends DefaultTableModel {

        private ArrayList listCellNotEditable;

        public CustomEditableCellTableModel() {
            super();
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(int numRows, int numColumns) {
            super(numRows, numColumns);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Vector columnNames, int numRows) {
            super(columnNames, numRows);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Object[] columnNames, int numRows) {
            super(columnNames, numRows);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Vector data, Vector columnNames) {
            super(data, columnNames);
            this.listCellNotEditable = new ArrayList();
        }

        public CustomEditableCellTableModel(Object[][] data, Object[] columnNames) {
            super(data, columnNames);
            this.listCellNotEditable = new ArrayList();
        }

        public void addNotEditableCell(int row, int column) {
            if ( ! (column >= 0 && column < this.getColumnCount()) ) {
                return;
            }
            if ( ! ((row >= 0) && (row < this.getRowCount())) ) {
                return;
            }
            this.listCellNotEditable.add(new Point(row, column));
        }

        public void removeNotEditableCell(int row, int column) {
            int index = this.listCellNotEditable.indexOf(new Point(row, column));
            if ( index != -1 ) {
                this.listCellNotEditable.remove(index);
            }
        }
       public boolean isCellEditable(int row, int column) {
            int index = this.listCellNotEditable.indexOf(new Point(row, column));
            if ( index == -1 ) {
                return ( false );
            } else {
                return ( true );
            }
        }
    }
public class carTab extends javax.swing.JPanel {
 public static JTable centrerTable(JTable table) {
        DefaultTableCellRenderer custom = new DefaultTableCellRenderer();//on crée un nouveau model de DefaultTableCellRenderer
        custom.setHorizontalAlignment(JLabel.CENTER); //dans celle ci on centre les header

        //on centre pour chaque cellule
        for (int i=0 ; i<table.getColumnCount() ; i++)
            table.getColumnModel().getColumn(i).setCellRenderer(custom);
        return table;
    }


    /** Creates new form painel */
    public carTab() throws ParseException {
        String données[][];
        initComponents();
                     if(GUI.LoginWindow.CanEditCarData)
             {
                         carAddButton.setEnabled(true);
                     }
                     else{carAddButton.setEnabled(false);}
        données=kernel.Car.searchcar(GUI.LoginWindow.sessionID,"3");

        //on affiche la jTable des utilisateurs'
        String[] nomsColonne={"ID","Klasse","Hersteller","Modell","Kennzeichen","Status","Sitzplätze","Kilometerstand","Erstzulassung","Kraftstofftyp","Preis"};
        CustomEditableCellTableModel jTable2 =new CustomEditableCellTableModel(données,nomsColonne) ;
        carTable.setModel(jTable2);
        carTable=centrerTable(carTable);
        carTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        carTable.setAutoCreateRowSorter(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        carTab = new javax.swing.JPanel();
        carSelectButton = new javax.swing.JButton();
        carTableScrollPane = new javax.swing.JScrollPane();
        carTable = new javax.swing.JTable();
        carAvaliableCheckBox = new javax.swing.JCheckBox();
        carNotAvaliableCheckBox = new javax.swing.JCheckBox();
        carSearchButton = new javax.swing.JButton();
        currentData = new GUI.currentDataFrame();
        carAddButton = new javax.swing.JButton();

        carSelectButton.setText("Auswählen"); // NOI18N
        carSelectButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carSelectButtonActionPerformed(evt);
            }
        });

        carTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "Klasse", "Hersteller", "Modell", "Kennzeichen", "Hersteller", "Status", "Sitzplätze", "Kilometerstand", "Erstzulassung", "Kraftstofftyp", "Preis"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        carTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                carTableMouseClicked(evt);
            }
        });
        carTableScrollPane.setViewportView(carTable);

        carAvaliableCheckBox.setSelected(true);
        carAvaliableCheckBox.setText("Verfügbar"); // NOI18N

        carNotAvaliableCheckBox.setSelected(true);
        carNotAvaliableCheckBox.setText("Nicht Verfügbar"); // NOI18N
        carNotAvaliableCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carNotAvaliableCheckBoxActionPerformed(evt);
            }
        });

        carSearchButton.setText("Suchen"); // NOI18N
        carSearchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carSearchButtonActionPerformed(evt);
            }
        });

        carAddButton.setText("hinzufügen"); // NOI18N
        carAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                carAddButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout carTabLayout = new javax.swing.GroupLayout(carTab);
        carTab.setLayout(carTabLayout);
        carTabLayout.setHorizontalGroup(
            carTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(carTabLayout.createSequentialGroup()
                .addGroup(carTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(carTabLayout.createSequentialGroup()
                        .addGap(442, 442, 442)
                        .addComponent(carAvaliableCheckBox)
                        .addGap(18, 18, 18)
                        .addComponent(carNotAvaliableCheckBox)
                        .addGap(18, 18, 18)
                        .addComponent(carSearchButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 774, Short.MAX_VALUE)
                        .addComponent(carAddButton))
                    .addGroup(carTabLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(carTableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 1650, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(currentData, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 1674, Short.MAX_VALUE)
            .addGroup(carTabLayout.createSequentialGroup()
                .addGap(521, 521, 521)
                .addComponent(carSelectButton, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(977, Short.MAX_VALUE))
        );
        carTabLayout.setVerticalGroup(
            carTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(carTabLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(carTabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(carNotAvaliableCheckBox)
                    .addComponent(carAvaliableCheckBox)
                    .addComponent(carSearchButton)
                    .addComponent(carAddButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(carTableScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(carSelectButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 113, Short.MAX_VALUE)
                .addComponent(currentData, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(carTab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addComponent(carTab, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
    }// </editor-fold>//GEN-END:initComponents

private void carTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_carTableMouseClicked
if(evt.getClickCount()>1){
             if(GUI.LoginWindow.CanEditCarData)
             {
                 this.setEnabled(false);
            carChange Framecar=new carChange();
                        String données[][];
            int Rowselect=carTable.getSelectedRow();
            Object id=carTable.getValueAt(Rowselect,0);
                try {
                    données = kernel.Car.searchcar(GUI.LoginWindow.sessionID, "3");
                } catch (ParseException ex) {
                    Logger.getLogger(carTab.class.getName()).log(Level.SEVERE, null, ex);
                }
           carChange.addupdate=2;
           Framecar.setTitle("ändern eine Fahrzeug");
           Framecar.carChangeIdLabel.setVisible(true);
           Framecar.carChangeIdInfo.setVisible(true);
           Framecar.carChangeIdInfo.setText(id.toString());
           Object MF=carTable.getValueAt(Rowselect,2);
           Framecar.carChangeManufacturerField.setText(MF.toString());
           Object Mo=carTable.getValueAt(Rowselect,3);
           Framecar.carChangeModelField.setText(Mo.toString());
           Object PF=carTable.getValueAt(Rowselect,6);
           Framecar.carChangePlacesField.setText(PF.toString());
           Object CPF=carTable.getValueAt(Rowselect,4);
           Framecar.carChangePlateField.setText(CPF.toString());
           Object CP=carTable.getValueAt(Rowselect,10);
           Framecar.carChangePriceFactorField.setText(CP.toString());
           Object Km=carTable.getValueAt(Rowselect,7);
           Framecar.carChangeKilometerField.setText(Km.toString());
           Object Cl=carTable.getValueAt(Rowselect,1);
           Framecar.carChangeCarClassField.setSelectedItem(Cl);
           Object BDF=carTable.getValueAt(Rowselect,8);
           Framecar.carChangeBuyDateField.setText(BDF.toString());
           Object Fuel=carTable.getValueAt(Rowselect,9);
           Framecar.carChangeFuelField.setSelectedItem(Fuel);
           Object Stat=carTable.getValueAt(Rowselect,5);
           Framecar.carChangeStatusField.setSelectedItem(Stat);
            Framecar.setSize(620,450);
            Framecar.setLocationRelativeTo(null);
            Framecar.setVisible(true);
            
            String data[][] = null;
                try {
                    data = kernel.Car.searchdamage(GUI.LoginWindow.sessionID, id.toString());
                } catch (ParseException ex) {
                    Logger.getLogger(carTab.class.getName()).log(Level.SEVERE, null, ex);
                }

        //on affiche la jTable des utilisateurs'
        String[] nomsColonne={"ID","Kundenid","Datum","Beschreibung","Status","Ersteller"};
        CustomEditableCellTableModel jTable2 =new CustomEditableCellTableModel(data,nomsColonne) ;
        carChange.carChangeDamageTable.setModel(jTable2);
        carChange.carChangeDamageTable=centrerTable(carChange.carChangeDamageTable);
        carChange.carChangeDamageTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        carChange.carChangeDamageTable.setAutoCreateRowSorter(true);
        }
}
}//GEN-LAST:event_carTableMouseClicked

private void carNotAvaliableCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carNotAvaliableCheckBoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_carNotAvaliableCheckBoxActionPerformed

private void carSearchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carSearchButtonActionPerformed
        String données[][] = null;
        String verfugbar = null;
        if(carNotAvaliableCheckBox.getSelectedObjects()!=null &&carAvaliableCheckBox.getSelectedObjects()!=null) verfugbar="3";
        if(carNotAvaliableCheckBox.getSelectedObjects()==null &&carAvaliableCheckBox.getSelectedObjects()!=null) verfugbar="1";
        if(carNotAvaliableCheckBox.getSelectedObjects()!=null &&carAvaliableCheckBox.getSelectedObjects()==null) verfugbar="0";
        System.out.println(verfugbar);
        try {
            données = kernel.Car.searchcar(GUI.LoginWindow.sessionID, verfugbar);
        } catch (ParseException ex) {
            Logger.getLogger(carTab.class.getName()).log(Level.SEVERE, null, ex);
        }

            //on affiche la jTable des utilisateurs'
            String[] nomsColonne = {"ID", "Klasse", "Hersteller", "Modell", "Kennzeichen", "Status", "Sitzplätze", "Kilometerstand", "Erstzulassung", "Kraftstofftyp", "Preis"};
            CustomEditableCellTableModel jTable2 = new CustomEditableCellTableModel(données, nomsColonne);
            carTable.setModel(jTable2);
            carTable = centrerTable(carTable);
            carTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
            carTable.setAutoCreateRowSorter(true); // TODO add your handling code here:

}//GEN-LAST:event_carSearchButtonActionPerformed

private void carAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carAddButtonActionPerformed
           carChange Framecar=new carChange();
           carChange.addupdate=1;
           Framecar.setTitle("Hinzufügen eine Fahrzeug");
           Framecar.carChangeIdLabel.setVisible(false);
           Framecar.carChangeIdInfo.setVisible(false);
           String Datum=new Date().toString();
           String year=Datum.substring(8,10);
           String FinalDatum=Datum.substring(0,8);
           Framecar.carChangeRegisterDateInfo.setText(FinalDatum+year);
           Framecar.carChangeManufacturerField.setText("");
           Framecar.carChangeModelField.setText("");
           Framecar.carChangePlacesField.setText("");
           Framecar.carChangePlateField.setText("");
           Framecar.carChangePriceFactorField.setText("");
           Framecar.carChangeKilometerField.setText("");
           Framecar.carChangeCarClassField.setSelectedIndex(0);
           Framecar.carChangeBuyDateField.setText("    -  -  ");
           Framecar.carChangeFuelField.setSelectedIndex(0);
           Framecar.carChangeStatusField.setSelectedIndex(0);
           Framecar.carChangeAddDamage.setVisible(false);
            Framecar.setSize(620,450);
            Framecar.setLocationRelativeTo(null);
            Framecar.setVisible(true);
            
            
}//GEN-LAST:event_carAddButtonActionPerformed

private void carSelectButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_carSelectButtonActionPerformed
// TODO add your handling code here:
    
    
    int[] selectedLines = carTable.getSelectedRows();
    ArrayList<Integer> selectedIds = new ArrayList<Integer>();
    
    for(int j=0; j<GraphicalUserInterface.selectedCar.size(); j++){
            selectedIds.add(GraphicalUserInterface.selectedCar.get(j).getID());
    }
    
    for(int i=0; i < selectedLines.length; i++){
        
        kernel.Car car = new kernel.Car();
        
        if(carTable.getValueAt(selectedLines[i], 5).equals("Verfügbar")){
                        
            String aux = new String();
            
            aux = (String) carTable.getValueAt(selectedLines[i], 0);
            car.setID(Integer.parseInt(aux));
           
            if(!selectedIds.contains(Integer.parseInt(aux))){
                aux = (String) carTable.getValueAt(selectedLines[i], 1);
                car.setFahrzeugklasse(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 2);
                car.setHersteller(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 3);
                car.setModell(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 4);
                car.setKennzeichen(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 5);
                car.setStatus(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 6);
                car.setSitzplatze(Integer.parseInt(aux));
                aux = (String) carTable.getValueAt(selectedLines[i], 7);
                car.setKilometerstand(Integer.parseInt(aux));
                aux = (String) carTable.getValueAt(selectedLines[i], 8);
                car.setErstzulassung(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 9);
                car.setKraftstofftyp(aux);
                aux = (String) carTable.getValueAt(selectedLines[i], 10);
                car.setFaktor(Float.parseFloat(aux));

                GraphicalUserInterface.selectedCar.add(car);      
            }
            else{
            JOptionPane.showMessageDialog(this, "Fahrzeug nummer "+carTable.getValueAt(selectedLines[i], 0)+" ist bereits ausgewählt!");
        }
            
  
        }
        else{
            JOptionPane.showMessageDialog(this, "Fahrzeug nummer "+carTable.getValueAt(selectedLines[i], 0)+" ist nich verfügbar!");
        }
            
    }

    currentData.updateCar();
    
    if(GraphicalUserInterface.selectedCustomer.getName()!=null) GraphicalUserInterface.enableRentalTab();
}//GEN-LAST:event_carSelectButtonActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton carAddButton;
    private javax.swing.JCheckBox carAvaliableCheckBox;
    private javax.swing.JCheckBox carNotAvaliableCheckBox;
    private javax.swing.JButton carSearchButton;
    private javax.swing.JButton carSelectButton;
    private javax.swing.JPanel carTab;
    public static javax.swing.JTable carTable;
    private javax.swing.JScrollPane carTableScrollPane;
    public static GUI.currentDataFrame currentData;
    // End of variables declaration//GEN-END:variables
}
